import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.atguigu.ggkt.utils.MapTool;
import org.yaml.snakeyaml.Yaml;

import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

public class WechatJsapiPayWithNacos {

    public static void main(String[] args) {
        WechatJsapiPayWithNacos.createFromNacos();
    }

    public static WechatPayConfigDemo createFromNacos(){
        // Nacos 服务器地址
        String serverAddr = "192.168.50.128:8848"; // 根据实际情况修改
        String namespace = "local";
        // 配置的数据 ID
        String dataId = "common-local.yaml";
        // 配置的组 ID
        String groupId = "LOCAL_GGKT_GROUP";

        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        properties.put("namespace", namespace);

        WechatPayConfigDemo wechatPayConfigDemo = null;
        try {
            // 创建 Nacos 配置服务实例
//            ConfigService configService = ConfigFactory.createConfigService(serverAddr);
            ConfigService configService = NacosFactory.createConfigService(properties);
            // 获取 YAML 配置
            String config = configService.getConfig(dataId, groupId, 5000);

            // 打印原始配置内容
            System.out.println("Config content: \n" + config);

//            wechatPayConfigDemo = new WechatPayConfigDemo();
            // 解析 YAML 配置
//            initWechatPayConfigDemo(config, wechatPayConfigDemo);
//            System.out.println("wechatPayConfigDemo: " + wechatPayConfigDemo.toString());
//            String property = getProperty(config, "spring.datasource.username");
            String property = getProperty(config, "spring.datasource.druid.initial-size");
//            String property = getProperty(config, "feign.client.config.default.connectTimeout");
            System.out.println("property: "+property);

        } catch (NacosException e) {
            e.printStackTrace();
        }
        return wechatPayConfigDemo;
    }

    // 方法用于从 YAML 配置中提取数据库 URL
    private static void initWechatPayConfigDemo(String yamlContent, WechatPayConfigDemo wechatPayConfigDemo) throws IOException {

        Yaml yaml = new Yaml();
        Map<String, Object> yamlMap = yaml.load(yamlContent);
        Map<String, Object> map = (Map<String, Object>)yamlMap.get("wechat");
        // 解析 wechat
        String mpAppId = MapTool.getStringFromMap(map,"mpAppId");
        String notifyUrl = MapTool.getStringFromMap(map,"notifyUrl");
        // 解析 wechat.mch
        Map<String, Object> mchMap = (Map<String, Object>)map.get("mch");
        // 解析 wechat.mch.xxx
        String mchId1 = MapTool.getStringFromMap(mchMap,"id-1");
        String privateKeyPath = MapTool.getStringFromMap(mchMap,"id-1-privateKeyPath");
        String merchantSerialNumber = MapTool.getStringFromMap(mchMap,"id-1-merchantSerialNumber");
        String apiV3Key = MapTool.getStringFromMap(mchMap,"id-1-apiV3Key");
        String signPem = MapTool.getStringFromMap(mchMap,"id-1-sign-pem");
        wechatPayConfigDemo.setMpAppId(mpAppId);
        wechatPayConfigDemo.setNotifyUrl(notifyUrl);
        wechatPayConfigDemo.setMchId1(mchId1);
        wechatPayConfigDemo.setPrivateKeyPath(privateKeyPath);
        wechatPayConfigDemo.setMerchantSerialNumber(merchantSerialNumber);
        wechatPayConfigDemo.setApiV3Key(apiV3Key);
        wechatPayConfigDemo.setSignPem(signPem);
    }

    public static String getProperty(String yamlContent, String key){

        Yaml yaml = new Yaml();
        Map<String, Object> yamlMap = yaml.load(yamlContent);

        String[] keys = key.split("\\.");
        int length = keys.length;

        for (int i = 0; i < keys.length; i++) {
            if (yamlMap == null) {
                return null;
            }

            if(i == length -1){
                return yamlMap.get(keys[i]).toString();
            }

            yamlMap = (Map<String, Object>)yamlMap.get(keys[i]);
        }
        return null;
    }

}
